﻿﻿ 
<%@ page contentType="text/html; charset=UTF-8" %>
<div class="searchBar">
权限名称：<input id="s_permission_name" name="s_permission_name" type="text" class="box-input">&nbsp;&nbsp;
<input id="s_permission" type="button" class="btns" value="查  询">
</div>

<table id="permission_grid"></table>      
<div id="permission_page"></div>  

<div id="permission_dialog_add" title="新增权限">
<form id="permission_form_add" name="permission_form_add" style="width:426px;" class="cmxform" method="POST">
    <div id="permission_add_errmsg"></div>
    <table width="100%" align="center" border="0" cellspacing="0" cellpadding="0" class="jkt-tableStyle">
      <tr>
        <td class="bluetd" align="right">权限名称：</td>
        <td class="bluetd" align="left" >
          <input id="permission_name_add" name="permission_name_add"/><span class="red-color">*</span>
          <br/>P_小模块名_页面名前3字母_(元素:页面元素名)
        </td>
     </tr>
      <tr>
        <td class="bluetd2" align="right">所属资源：</td>
        <td class="bluetd2" align="left">
          <input id="permission_resource_add" name="permission_resource_add"/><span class="red-color">*</span>
        </td>
     </tr>
      <tr>
        <td class="bluetd" align="right">描述：</td>
        <td class="bluetd" align="left">
          <input id="permission_description_add" name="permission_description_add"/>
        </td>
     </tr>
      <tr>
        <td class="bluetd" align="right">分类：</td>
        <td class="bluetd" align="left">
          <select style="width:140px" id="permission_sort_add" name="permission_sort_add">
          <option value="-1">——请选择——</option>
          <option value="0">菜单</option>
          <option value="1">行为</option>
          <option value="2">元素</option>
          <option value="3">其它</option>
        </select><span class="red-color">*</span>
        </td>
     </tr>
      <tr>
        <td colspan="2" align="center">
         <input type="button" class="btns" id="permission_add_bt" value="提  交" style="margin-top:8px;" />&nbsp;&nbsp;
         <input type="button" class="btns" id="permission_add_close" value="关  闭" style="margin-top:8px;" />
      </td>
</tr>
</table>
</form>
</div> 


<div id="permission_dialog_up" title="修改权限">
<form id="permission_form_up" name="permission_form_up" style="width:426px;" class="cmxform" method="POST">
    <div id="permission_up_errmsg"></div>
    <table width="100%" align="center" border="0" cellspacing="0" cellpadding="0" class="jkt-tableStyle">
      <tr>
        <td class="bluetd" align="right">权限名称：</td>
        <td class="bluetd" align="left" >
          <input id="permission_name_up" name="permission_name_up" readonly/><span class="red-color">*</span>
        </td>
     </tr>
      <tr>
        <td class="bluetd2" align="right">所属资源：</td>
        <td class="bluetd2" align="left">
          <input id="permission_resource_up" name="permission_resource_up"/>
        </td>
     </tr>
      <tr>
        <td class="bluetd" align="right">描述：</td>
        <td class="bluetd" align="left">
          <input id="permission_description_up" name="permission_description_up"/>
        </td>
     </tr>
      <tr>
        <td class="bluetd" align="right">分类：</td>
        <td class="bluetd" align="left">
          <select style="width:140px" id="permission_sort_up" name="permission_sort_up">
          <option value="-1">——请选择——</option>
          <option value="0">菜单</option>
          <option value="1">行为</option>
          <option value="2">元素</option>
          <option value="3">其它</option>
        </select><span class="red-color">*</span>
        </td>
     </tr>
      <tr>
        <td colspan="2" align="center">
         <input type="button" class="btns" id="permission_up_bt" value="提  交" style="margin-top:8px;" />&nbsp;&nbsp;
         <input type="button" class="btns" id="permission_up_close" value="关  闭" style="margin-top:8px;" />
      </td>
</tr>
</table>
</form>
</div> 

<script>

    var permissionaddvalidate,permissionupvalidate,permissionids='';//本页面变量
    
	jQuery("#permission_grid").jqGrid({//列表
        datatype: "json", 
        hidegrid: false, //不显示收缩
        url:'system/Permission/list.do',
        height:permissiongridHeight,
        forceFit: true,//调整宽度不改变表格宽度
        width: permissiongridWidth,
        colNames:[
        '编号'
        ,'权限名称'
        ,'所属资源'
        ,'描述'
        //,'父级id'
        ,'分类'
        ],
        colModel:[
        {name:'id',index:'id', width:2,sorttype:'int',sortable:false}  
        ,{name:'name',index:'name',width:120,sortable:false}
        ,{name:'resource',index:'resource',width:150,sortable:false}
        ,{name:'description',index:'description',width:180,sortable:false}
        //,{name:'parentId',index:'parentId',width:90,sortable:false}
        ,{name:'sort',index:'sort',width:90,sortable:false,formatter:fun_permission_formatSort}
        ],
        multiselect: true,//禁止多选
        toolbar:[true,'top'],
        rowNum:10, 
        rowList:[5,10,20], 
        viewrecords: true, 
        sortname:  "id " , //默认排序的列名    
        sortorder: "desc",            //排序方式asc升序 desc 降序
        pager: '#permission_page', 
        jsonReader: {     
          root: 'dataRows',   
          repeatitems : false     
        }
    })
    
    $("#t_permission_grid").append("<div style='width:30%;' class='l'>系统管理->权限列表</div><div class='r text-r' style='width:60%; height:28px'>"
       +"<a href='javascript:void(0)' onclick=\"fun_permission_update()\" class='modify r'>修改</a>"
       +"<a href='javascript:void(0)' onclick=\"fun_permission_delete()\" class='del r'>删除</a> "
       +"<a href='javascript:void(0)' onclick=\"fun_permission_add()\" class='add r'>新增</a></div>");//列表头
       
    $("#t_permission_grid").removeClass("ui-userdata ui-state-default"); //删样式
    $("#t_permission_grid").addClass("ui-title ui-corner-top ui-helper-clearfix");//加样式
    
    jQuery("#permission_grid").jqGrid('hideCol',['id']);//隐藏列
    $("#permission_grid").closest(".ui-jqgrid-bdiv").css({ 'overflow-x' : 'hidden' });//隐藏横向滚动条
    
    function fun_permission_formatSort(cellvalue, options, rowObject){
        if(rowObject.sort==0){
            return ("菜单");
        }else if(rowObject.sort==1){
            return ("行为");
        }else if(rowObject.sort==2){
            return ("元素");
        }else{
            return ("其他");
        }
    }
    
    function fun_permission_add(){//新增操作
		$("#permission_form_add")[0].reset();
		$('#permission_add_errmsg').html("");
		$("#permission_dialog_add").dialog("open");
	}
    
    $('#permission_add_bt').click(function(){//点击按妞
         if(rulesByValidator(permissionaddvalidate)){
             permission_add_submit();
         }
     })
     
     function  permission_add_submit(){//新增提交
        var permissionJson="{'id':'','name':'"+$('#permission_name_add').val()+"','resource':'"+$('#permission_resource_add').val()+"','description':'"+$('#permission_description_add').val()+"','parentId':'0','sort':'"+$('#permission_sort_add').val()+"'}";  
         $.ajax({
             type: "POST",
             url:"system/Permission/save.do",
             data:{
                 permissionJson:permissionJson
             },
             dataType: "json",
             contentType: "application/x-www-form-urlencoded; charset=UTF-8",
             success:function(json){
                 if(json.success){
                     $("#permission_grid").trigger("reloadGrid");
                     alertmsg(json.msg,"info");
                 }else{
                     alertmsg(json.msg,"alert");
                 }
                 $("#permission_dialog_add").dialog('close');
             }
         })
     }
     
     $('#permission_add_close').click(function(){//关闭
         $("#permission_dialog_add").dialog('close');
     })
     
     $('#permission_up_bt').click(function(){//点击按妞
         if(rulesByValidator(permissionupvalidate)){
             permission_update_submit();
         }
     })
        
     function  permission_update_submit(){//修改提交
         var permissionJson="{'id':'"+rowData.id+"','name':'"+$('#permission_name_up').val()+"','resource':'"+$('#permission_resource_up').val()+"','description':'"+$('#permission_description_up').val()+"','parentId':'0','sort':'"+$('#permission_sort_up').val()+"'}";  
         $.ajax({
             type: "POST",
             url:"system/Permission/update.do",
             data:{
                 permissionJson:permissionJson
             },
             dataType: "json",
             contentType: "application/x-www-form-urlencoded; charset=UTF-8",
             success:function(json){
                 if(json.success){
                     $("#permission_grid").trigger("reloadGrid");
                     alertmsg(json.msg,"info");
                 }else{
                     alertmsg(json.msg,"alert");
                 }
                 $("#permission_dialog_up").dialog('close');
             }
        })
     }
        
     $('#permission_up_close').click(function(){//关闭
         $("#permission_dialog_up").dialog('close');
     })
	
	 permissionaddvalidate = $("#permission_form_add").validate({//验证
	        errorElement: "div",
            errorPlacement: function(error, element) {
			    $('#permission_add_errmsg').html("");
				error.appendTo( "#permission_add_errmsg" );
			},
			success: function(label) {
			    label.text("验证通过!").removeClass("error").addClass("success");
			},
	        rules: {
	            permission_name_add:{
                   required: true,
                   notEqual:"P_NAME",
                   begin:"P_",
                   remote: {
                       url: "system/Permission/checkPermissionName.do",
                       type: "post",
                       data: {permissionName: function() {
                           return $("#permission_name_add").val()}
                       }
                   },
                   stringMaxLength:50
               },
               permission_resource_add:{
                   required: true,
                   stringMaxLength:50
               },
               permission_sort_add:{
                   notEqual: "-1"
               },
               permission_description_add:{
                   stringMaxLength:500
               }
	        }, 
	        messages: {
	            permission_name_add:{
                   required: "权限名称不能为空",
                   notEqual: "格式:R_NAME",
                   begin: "必须以P_开头",
                   remote: "权限名称已存在",
                   stringMaxLength:"权限名称长度最长不能超过50位"
                },
                permission_resource_add:{
                   required: "所属资源不能为空",
                   stringMaxLength:"所属资源长度最长不能超过60位"
                },
                permission_sort_add:{
                   notEqual: "权限类型不能为空"
               },
                permission_description_add:{
                   stringMaxLength:"描述长度最长不能超过500位"
               }
	        },
	        onkeyup:false,
		    onsubmit:false
	})
	
	permissionupvalidate = $("#permission_form_up").validate({//验证
	        errorElement: "div",
            errorPlacement: function(error, element) {
			    $('#permission_up_errmsg').html("");
				error.appendTo( "#permission_up_errmsg" );
			},
			success: function(label) {
			    label.text("验证通过!").removeClass("error").addClass("success");
			},
	        rules: {
	            permission_resource_up:{
                   required: true
                },
               permission_sort_up:{
                   required: true
               }
	        }, 
	        messages: {
	            permission_resource_up:{
                   required: "所属资源不能为空"
                },
                permission_sort_up:{
                   required: "权限类型不能为空"
               }
	        },
	        onkeyup:false,
		    onsubmit:false
	})
	
	function fun_permission_delete(){//删除验证
        var data = $("#permission_grid").jqGrid('getGridParam','selarrrow');//'selrow'
        var length=data.length;
        if(length <1){
	        alertmsg("请至少选择一条记录再操作!","alert");
	        return;
	    }
	    permissionids='';
	    for(var i=0;i<length;i++){
	        if(i > 0){
                 permissionids += ',';
            }
            permissionids += data[i];
	    }
        confirm("确认要删除所选记录吗？", fun_permission_del);
    }
    
    function fun_permission_del(){//删除操作
	    $.ajax({
            type: "POST",
            url:"system/Permission/delete.do",
            dataType: "json",
            data:{
                 ids:permissionids
            },
            success:function(json){
                 if(json.success){
                      alertmsg(json.msg,"info");
                      $("#permission_grid").trigger("reloadGrid");
                 }else{
                      alertmsg(json.msg,"alert");
                 }
            }
        })
    }
    
    function fun_permission_update(){//修改操作
        var dataUpdate = $("#permission_grid").jqGrid('getGridParam','selarrrow');//'selarrrow'
        var lengthUpdate=dataUpdate.length;
        if(lengthUpdate >1){
  	        alertmsg("每次只能修改一条记录!","alert");
  	        return;
  	    }
        var id =$("#permission_grid").jqGrid('getGridParam','selrow');
        if(typeof(id) =="string"){
            rowData=jQuery("#permission_grid").jqGrid('getRowData',id);  
		    $("#permission_form_up")[0].reset();
		    permissionupvalidate.resetForm(); //重置验证状态
		    $('#permission_up_errmsg').html("");
		    $("#permission_dialog_up").dialog("open");
            load_permission_data();
        }else{
           alertmsg("请至少选择一条记录再操作!","alert");
        }
	}
	
	function load_permission_data(){//加载数据
         $("#permission_name_up").val(rowData.name);
         $("#permission_resource_up").val(rowData.resource);
         $("#permission_description_up").val(rowData.description);
         //$("#permission_parentId_index").val(rowData.parentId);
         if(rowData.sort=="菜单"){
             $("#permission_sort_up").val(0);
         }else if(rowData.sort=="行为"){
             $("#permission_sort_up").val(1);
         }else if(rowData.sort=="元素"){
             $("#permission_sort_up").val(2);
         }else{
             $("#permission_sort_up").val(3);
         }
         //$("#permission_sort_up option[text='元素']").attr("selected", "selected"); 
	}
	
	$("#permission_dialog_add").dialog({//弹出层
		 autoOpen: false,
		 height: permissionDialogHeight(),
		 width: 470,
		 modal: true
	})
	
	$("#permission_dialog_up").dialog({//弹出层
		 autoOpen: false,
		 height: permissionDialogHeight(),
		 width: 470,
		 modal: true
	})

    function permissionDialogHeight(){//弹出层高度
        if($.browser.mozilla){//火狐
           return 300;
	    }else{//其它
		   return 300;
	    }
    }

	function fun_permissiongridResize(){
	    jQuery("#permission_grid").setGridHeight(mDiv.height-195);
        jQuery("#permission_grid").setGridWidth(mDiv.width-22);
	}
	
	$("#westDiv").bind("pageResize",function(event){
		 fun_permissiongridResize();
	})
	
	function permissiongridWidth(){    
        mDiv.width-22; 
    }
    
    function permissiongridHeight(){    
        mDiv.height-195;
    }
	
	fun_permissiongridResize();
	
	$('#s_permission').click(function(){
         var s_name=$('#s_permission_name').val();
         $("#permission_grid").setGridParam({url:'system/Permission/list.do?s_name='+s_name});
         $("#permission_grid").trigger("reloadGrid");
    })

</script>
